package com.zhoug.common.content;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;

/**
 * 文件类型
 *
 * @Author HK-LJJ
 * @Date 2019/12/6
 * @Description
 */
public class FileType {
    private static final String TAG = ">>>FileType";
    /**
     * 未知
     */
    public static final int UNKNOWN = -1;

    /**
     * 所有类型
     */
    public static final int ALL = -2;

    /**
     * 图片
     */
    public static final int IMAGE = 1;
    /**
     * 视频
     */
    public static final int VIDEO = 2;
    /**
     * 音频
     */
    public static final int AUDIO = 3;
    /**
     * doc/docx
     */
    public static final int WORD = 4;
    /**
     * excel
     */
    public static final int EXCEL = 5;
    /**
     * pdf
     */
    public static final int PDF = 6;
    /**
     * ppt
     */
    public static final int PPT = 7;

    /**
     * txt
     */
    public static final int TXT = 8;
    /**
     * html
     */
    public static final int HTML = 9;
    /**
     * xml
     */
    public static final int XML = 10;
    /**
     * 压缩文件
     */
    public static final int ZIP = 11;


    //w未知
    public static final String MIME_TYPE_UNKNOWN = "*/*";
    //图片
    public static final String MIME_TYPE_JPG = "image/jpeg";
    public static final String MIME_TYPE_PNG = "image/png";
    public static final String MIME_TYPE_BMP = "image/bmp";
    public static final String MIME_TYPE_GIF = "image/gif";
    public static final String MIME_TYPE_WEBP = "image/webp";
    //视频
    public static final String MIME_TYPE_MP4 = "video/mp4";
    public static final String MIME_TYPE_3GPP = "video/3gpp";
    public static final String MIME_TYPE_3GPP2 = "video/3gpp2";
    public static final String MIME_TYPE_AVI = "video/x-msvideo";
    public static final String MIME_TYPE_MKV = "video/x-matroska";
    public static final String MIME_TYPE_WEBM = "video/webm";
    public static final String MIME_TYPE_MOV = "video/quicktime";
    //音频
    public static final String MIME_TYPE_MP3 = "audio/mpeg";
    public static final String MIME_TYPE_AAC = "audio/aac";
    public static final String MIME_TYPE_OGG = "audio/ogg";
    public static final String MIME_TYPE_AMR = "audio/amr";
    public static final String MIME_TYPE_WAV = "audio/x-wav";
    public static final String MIME_TYPE_FLAC = "audio/flac";
    //文本
    public static final String MIME_TYPE_TXT = "text/plain";
    public static final String MIME_TYPE_HTML = "text/html";
    public static final String MIME_TYPE_CSS = "text/css";
    public static final String MIME_TYPE_JS = "application/javascript";
    public static final String MIME_TYPE_JSON = "application/json";
    public static final String MIME_TYPE_XML = "application/xml";
    //文档
    public static final String MIME_TYPE_DOC = "application/msword";
    public static final String MIME_TYPE_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String MIME_TYPE_XLS = "application/vnd.ms-excel";
    public static final String MIME_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String MIME_TYPE_PPT = "application/vnd.ms-powerpoint";
    public static final String MIME_TYPE_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    public static final String MIME_TYPE_PDF = "application/pdf";
    //压缩文件
    public static final String MIME_TYPE_ZIP = "application/zip";
    public static final String MIME_TYPE_RAR = "application/x-rar-compressed";
    public static final String MIME_TYPE_7Z = "application/x-7z-compressed";
    public static final String MIME_TYPE_TAR = "application/x-tar";
    public static final String MIME_TYPE_GZ = "application/gzip";


    // 扩展MIME类型映射表
    private static final Map<String, String> MIME_TYPE_MAP = new HashMap<String, String>() {{
        put("jpg", MIME_TYPE_JPG);
        put("jpeg", MIME_TYPE_JPG);
        put("png", MIME_TYPE_PNG);
        put("bmp", MIME_TYPE_BMP);
        put("gif", MIME_TYPE_GIF);
        put("webp", MIME_TYPE_WEBP);

        put("mp4", MIME_TYPE_MP4);
        put("3gp", MIME_TYPE_3GPP);
        put("3gpp", MIME_TYPE_3GPP);
        put("3gpp2", MIME_TYPE_3GPP2);
        put("avi", MIME_TYPE_AVI);
        put("mkv", MIME_TYPE_MKV);
        put("webm", MIME_TYPE_WEBM);
        put("mov", MIME_TYPE_MOV);


        put("mp3", MIME_TYPE_MP3);
        put("aac", MIME_TYPE_AAC);
        put("ogg", MIME_TYPE_OGG);
        put("amr", MIME_TYPE_AMR);
        put("wav", MIME_TYPE_WAV);
        put("flac", MIME_TYPE_FLAC);


        put("txt", MIME_TYPE_TXT);
        put("html", MIME_TYPE_HTML);
        put("htm", MIME_TYPE_HTML);
        put("css", MIME_TYPE_CSS);
        put("js", MIME_TYPE_JS);
        put("json", MIME_TYPE_JSON);
        put("xml", MIME_TYPE_XML);

        put("doc", MIME_TYPE_DOC);
        put("docx", MIME_TYPE_DOCX);
        put("xls", MIME_TYPE_XLS);
        put("xlsx", MIME_TYPE_XLSX);
        put("ppt", MIME_TYPE_PPT);
        put("pptx", MIME_TYPE_PPTX);
        put("pdf", MIME_TYPE_PDF);

        put("zip", MIME_TYPE_ZIP);
        put("rar", MIME_TYPE_RAR);
        put("7z", MIME_TYPE_7Z);
        put("tar", MIME_TYPE_TAR);
        put("gz", MIME_TYPE_GZ);
    }};


    /**
     * 获取文件的mimeType 用于设置intent.setDataAndType(uri,type) 中的type;
     *
     * @param path  包含后缀的路径 eg:sdcard/img/12.png 或者文件后缀 eg: png
     * @return 中的type
     */
    public static @NonNull String getMimeType(String path) {
        LogUtils.d(TAG, "path:" + path);
        String mimeType;
        if (TextUtils.isEmpty(path)) {
            mimeType = MIME_TYPE_UNKNOWN;
        } else {
            //获取文件扩展名
            String fileExtension = getFileExtension(path);
            LogUtils.d(TAG, "fileExtension:" + fileExtension);
            if (TextUtils.isEmpty(fileExtension)) {
                mimeType = MIME_TYPE_UNKNOWN;
            } else {
                // 首先尝试使用Android系统API获取MIME类型
                mimeType = getMimeTypeFromSystem(fileExtension);
                LogUtils.d(TAG, "FromSystem mimeType:" + mimeType);
                if (TextUtils.isEmpty(mimeType) || mimeType.equals(MIME_TYPE_UNKNOWN)) {
                    //如果系统API无法识别 使用自定义映射表获取
                    mimeType = MIME_TYPE_MAP.get(fileExtension.toLowerCase());
                }
            }
        }
        LogUtils.d(TAG, "mimeType:" + mimeType);
        return !TextUtils.isEmpty(mimeType) ? mimeType : MIME_TYPE_UNKNOWN;
    }


    /**
     * 通过文件后缀获取文件类型
     *
     * @param path 包含后缀的路径 eg:sdcard/img/12.png 或者文件后缀 eg: png
     * @return {@link #IMAGE,#VIDEO,#AUDIO,#WORD,#PDF,#TXT ,#UNKNOWN}
     */
    public static int getType(String path) {
        if (path == null) {
            return UNKNOWN;
        }
        String fileExtension = getFileExtension(path);
        if (StringUtils.isNullOrEmpty(fileExtension)) {
            return UNKNOWN;
        }
        fileExtension = fileExtension.toLowerCase();
        if ("jpg".equals(fileExtension) ||
                "jpeg".equals(fileExtension) ||
                "png".equals(fileExtension) ||
                "bmp".equals(fileExtension) ||
                "gif".equals(fileExtension) ||
                "webp".equals(fileExtension)) {
            return IMAGE;
        }
        if ("mp4".equals(fileExtension) ||
                "3gp".equals(fileExtension) ||
                "3gpp".equals(fileExtension) ||
                "3gpp2".equals(fileExtension) ||
                "avi".equals(fileExtension) ||
                "mkv".equals(fileExtension) ||
                "webm".equals(fileExtension) ||
                "mov".equals(fileExtension)) {
            return VIDEO;
        }
        if ("mp3".equals(fileExtension) ||
                "aac".equals(fileExtension) ||
                "ogg".equals(fileExtension) ||
                "amr".equals(fileExtension) ||
                "wav".equals(fileExtension) ||
                "flac".equals(fileExtension)) {
            return AUDIO;
        }
        if ("doc".equals(fileExtension) ||
                "docx".equals(fileExtension)) {
            return WORD;
        }
        if ("xls".equals(fileExtension) ||
                "xlsx".equals(fileExtension)) {
            return EXCEL;
        }
        if ("ppt".equals(fileExtension) ||
                "pptx".equals(fileExtension)) {
            return PPT;
        }
        if ("pdf".equals(fileExtension)) {
            return PDF;
        }
        if ("txt".equals(fileExtension)) {
            return TXT;
        }
        if ("html".equals(fileExtension) ||
                "htm".equals(fileExtension)) {
            return HTML;
        }
        if ("xml".equals(fileExtension)) {
            return XML;
        }
        if ("zip".equals(fileExtension) ||
                "rar".equals(fileExtension) ||
                "7z".equals(fileExtension) ||
                "tar".equals(fileExtension) ||
                "gz".equals(fileExtension)) {
            return ZIP;
        }
        return UNKNOWN;
    }


    /**
     * 使用Android系统API获取MIME类型
     *
     * @param fileExtension 文件扩展名 eg: png/pdf
     * @return MimeType
     */
    private static String getMimeTypeFromSystem(@NonNull String fileExtension) {
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase());
    }


    /**
     * 获取文件扩展名(后缀)
     *
     * @param path 包含后缀的路径 eg: sdcard/img/12.png 或者文件后缀 eg: png
     * @return png
     */
    private static String getFileExtension(String path) {
        if (TextUtils.isEmpty(path)) return null;
        int index = path.lastIndexOf(".");
        if (index >= 0 && index < path.length() - 1) {
            return path.substring(index + 1);
        } else {
            return path;
        }
    }

}
